/**
 * Created by Administrator on 2018/3/30 0030.
 */
let sqlModel = require('../lib/mysql')
const {handleError, handleSuccess} = require('../units')

class userController {
    static async getUsers(ctx) {
        const result = await  sqlModel.selectUser();
        if (result.length) {
            handleSuccess({ctx, data: result})
        } else {
            handleError({ctx, message: "还未有用户注册"})
        }
        console.log(result)
    }

    static async login(ctx) {
        const {name,password} = ctx.request.body;
        const result = await sqlModel.findByName(name)
        console.log(result)
        if (result.length) {
            if (result[0].password == password) {
                handleSuccess({ctx, data: result})
            } else {
                handleError({ctx, message: "密码错误"})
            }
        } else {
            handleError({ctx, message: "账号不存在"})
        }

    }

    static async resgister(ctx) {
        const {name, password} = ctx.request.body;
        const result = await sqlModel.findByName(name)
        if (result.length) {
            handleError({ctx, message: "账号已注册"})
        } else {
            const resgister = await sqlModel.insertUser([name, password])
            if (resgister) {
                handleSuccess({ctx, data: null,message:"注册成功"})
            }
        }
    }
    static  async addUserInfo(){
        const userInfo = ctx.request.body;

    }
}

module.exports = userController